package com.demo.algorithm.demo4;

import java.io.IOException;
import java.util.concurrent.locks.LockSupport;

/**
 * <p>
 * 两个线程交替输出
 * </p>
 *
 * @author yanyg
 * @date 2024/2/16 15:03
 */
public class T01_LockSupport {
    static Thread t1;
    static Thread t2;

    public static void main(String[] args) throws IOException {
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        t1 = new Thread(() -> {
            for (char c : aI) {
                System.out.print(c);
                // 叫醒
                LockSupport.unpark(t2);
                // 挂起
                LockSupport.park();
            }
        }, "t1");

        t2 = new Thread(() -> {
            for (char c : aC) {
                // 挂起
                LockSupport.park();
                System.out.print(c);
                // 叫醒
                LockSupport.unpark(t1);
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
